﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Celeriq.Profiler
{
	public partial class MDIParent : Form
	{
		private int childFormNumber = 0;

		public MDIParent()
		{
			InitializeComponent();
			EnableMenus();
		}

		private void OpenFile(object sender, EventArgs e)
		{
			var openFileDialog = new OpenFileDialog();
			openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
			openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
			if (openFileDialog.ShowDialog(this) == DialogResult.OK)
			{
				var FileName = openFileDialog.FileName;
			}
		}

		#region Menu Handlers

		private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			var saveFileDialog = new SaveFileDialog();
			saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
			saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
			if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
			{
				var FileName = saveFileDialog.FileName;
			}
		}

		private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.Cascade);
		}

		private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.TileVertical);
		}

		private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.TileHorizontal);
		}

		private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.ArrangeIcons);
		}

		private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
		{
			foreach (var childForm in MdiChildren)
			{
				childForm.Close();
			}
		}

		#endregion

		private void EnableMenus()
		{
			newToolStripButton.Enabled = true;
			var child = this.ActiveMdiChild as MainForm;
			if (child == null)
			{
				playToolStripButton.Enabled = false;
				stopToolStripButton.Enabled = false;
			}
			else
			{
				playToolStripButton.Enabled = !child.IsRunning;
				stopToolStripButton.Enabled = child.IsRunning;
			}
		}

		private void OpenConnection()
		{
			var F = new ServerConnectionForm();
			if (F.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				var childForm = new MainForm();
				childForm.MdiParent = this;
				childForm.Text = "Window " + childFormNumber++;
				childForm.Show();

				childForm.Credentials = F.Credentials;
				childForm.PublicKey = F.PublicKey;
				childForm.ServerName = F.ServerName;
				childForm.StartProfiling();

				this.EnableMenus();
			}
		}

		private void newToolStripButton_Click(object sender, EventArgs e)
		{
			OpenConnection();
		}

		private void playToolStripButton_Click(object sender, EventArgs e)
		{
			var child = this.ActiveMdiChild as MainForm;
			if (child != null)
			{
				child.IsRunning = true;
				EnableMenus();
			}
		}

		private void StopToolStripButton_Click(object sender, EventArgs e)
		{
			var child = this.ActiveMdiChild as MainForm;
			if (child != null)
			{
				child.IsRunning = false;
				EnableMenus();
			}
		}

	}
}
